package com.fw4j.web.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public abstract class WebService {
	protected URL url = null;

	protected Document SOAP1_1(String url,String SOAPAction,String args) throws IOException, ParserConfigurationException, SAXException{
		this.url = new URL(url);
		URLConnection conn = this.url.openConnection();
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		conn.setRequestProperty("SOAPAction", SOAPAction);
		conn.setRequestProperty("Content-Length",String.valueOf(args.getBytes().length));
		conn.setDoInput(true);
		conn.setDoOutput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(args);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuffer result= new StringBuffer();
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        out.close();
        in.close();
        System.out.println(result.toString());
        String xmlStr = result.toString();
        xmlStr = xmlStr.replace("&lt;", "<");
        xmlStr = xmlStr.replace("&gt;", ">");
        xmlStr = xmlStr.replace("&quot;", "\"");
        xmlStr = xmlStr.replace("&amp;", "&");
        xmlStr = xmlStr.replace("&apos;", "'");
        xmlStr = xmlStr.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
        DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = bf.newDocumentBuilder();
        StringReader sr = new StringReader(xmlStr);
        InputSource isq = new InputSource(sr);
        Document document = db.parse(isq);
        return document;
	}
	
	protected Document SOAP1_2(String url,String args) throws IOException, ParserConfigurationException, SAXException{
		this.url = new URL(url);
		URLConnection conn = this.url.openConnection();
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		conn.setRequestProperty("Content-Length",String.valueOf(args.getBytes().length));
		conn.setDoInput(true);
		conn.setDoOutput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(args);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuffer result= new StringBuffer();
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        out.close();
        in.close();
        System.out.println(result.toString());
        String xmlStr = result.toString();
        xmlStr = xmlStr.replace("&lt;", "<");
        xmlStr = xmlStr.replace("&gt;", ">");
        xmlStr = xmlStr.replace("&quot;", "\"");
        xmlStr = xmlStr.replace("&amp;", "&");
        xmlStr = xmlStr.replace("&apos;", "'");
        xmlStr = xmlStr.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
        DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = bf.newDocumentBuilder();
        StringReader sr = new StringReader(xmlStr);
        InputSource isq = new InputSource(sr);
        Document document = db.parse(isq);
        return document;
	}
	
	protected String sendGET(String url,String args)throws IOException, ParserConfigurationException, SAXException{
	
		this.url = new URL(url+"?"+args);
		URLConnection conn = this.url.openConnection();
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.connect();
		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuffer result= new StringBuffer();
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        in.close();
        System.out.println(result.toString());
        return result.toString();	
	}
	
	protected String sendPOST(String url,String args)throws IOException, ParserConfigurationException, SAXException{
		this.url = new URL(url);
		URLConnection conn = this.url.openConnection();
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("connection", "Keep-Alive");
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length",String.valueOf(args.getBytes().length));
		conn.setDoInput(true);
		conn.setDoOutput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(args);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuffer result= new StringBuffer();
        while ((line = in.readLine()) != null) {
            result.append(line);
        }
        out.close();
        in.close();
        System.out.println(result.toString());
        return result.toString();
	}
}
